package com.ddm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddm.dto.webDTO.StandardDTO;
import com.ddm.entity.webEntity.StandardFile;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface StandardFileMapper extends BaseMapper<StandardFile> {

    // 根据ID查询单个标准文件，返回 StandardDTO
    @Select("SELECT " +
            "s.id, " +
            "s.level, " +
            "s.image, " +
            "s.standard_name AS standardName, " +
            "s.scope, " +
            "s.publication_date AS publicationDate, " +
            "s.responsible_unit AS responsibleUnit, " +
            "f.file AS filePath ," +
            "f.file_name AS fileName  "+
            "FROM standard s " +
            "LEFT JOIN file f ON s.id = f.standard_id " +  // 关联条件正确
            "WHERE s.id = #{id}")
    StandardDTO selectStandardFileById(Integer id);

    @Select({"<script>",
            "SELECT ",
            "s.id, ",
            "s.level, ",
            "s.image, ",
            "s.standard_name AS standardName, ",
            "s.scope, ",
            "s.publication_date AS publicationDate, ",
            "s.responsible_unit AS responsibleUnit, ",
            "f.file AS filePath , ",
            "f.file_name AS fileName ",
            "FROM standard s ",
            "LEFT JOIN file f ON s.id = f.standard_id ",
            "<where>",
            "  <if test=\"level != null and level != ''\">",
            "    s.level = #{level}",
            "  </if>",
            "  <if test=\"key != null and key != ''\">",
            "    AND (",
            "      s.standard_name LIKE CONCAT('%', #{key}, '%') ",
            "      OR s.standard_number LIKE CONCAT('%', #{key}, '%') ",
            "      OR s.department_name LIKE CONCAT('%', #{key}, '%')",
            "    )",
            "  </if>",
            "</where>",
            "ORDER BY s.id DESC",
            "</script>"})
    IPage<StandardDTO> selectStandardByPage(Page<StandardDTO> page,
                                            @Param("level") String level,
                                            @Param("key") String key);

    // 新增：查询用户收藏的标准文件（返回DTO分页）
    @Select({"<script>",
            "SELECT ",
            "s.id, ",
            "s.level, ",
            "s.image, ",
            "s.standard_name AS standardName, ",
            "s.scope, ",
            "s.publication_date AS publicationDate, ",
            "s.responsible_unit AS responsibleUnit, ",
            "f.file AS filePath , ",
            "f.file_name AS fileName ",
            "FROM standard s ",
            "LEFT JOIN file f ON s.id = f.standard_id ",
            "WHERE s.id IN ",
            "(",
            "   SELECT standard_id FROM bookmark WHERE user_id = #{userId} AND standard_id IS NOT NULL",
            ")",
            "<if test=\"key != null and key != ''\">",
            "AND (",
            "   s.standard_name LIKE CONCAT('%', #{key}, '%') ",
            "   OR s.responsible_unit LIKE CONCAT('%', #{key}, '%') ",
            "   OR s.level LIKE CONCAT('%', #{key}, '%')",
            ")",
            "</if>",
            "ORDER BY s.id DESC",
            "</script>"})
    IPage<StandardDTO> selectCollectedStandardByUserId(
            Page<StandardDTO> page,
            @Param("userId") Integer userId,
            @Param("key") String key
    );
}